home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / Fl_Roller.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-15  |  4.3 KB  |  142 lines

  1. //
  2. // "$Id: Fl_Roller.cxx,v 1.6.2.1 1999/09/15 16:07:03 mike Exp $"
  3. //
  4. // Roller widget for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. // Rapid-App style knob
  27.  
  28. #include <FL/Fl.H>
  29. #include <FL/Fl_Roller.H>
  30. #include <FL/fl_draw.H>
  31. #include <math.h>
  32.  
  33. int Fl_Roller::handle(int event) {
  34.   static int ipos;
  35.   int newpos = horizontal() ? Fl::event_x() : Fl::event_y();
  36.   switch (event) {
  37.   case FL_PUSH:
  38.     handle_push();
  39.     ipos = newpos;
  40.     return 1;
  41.   case FL_DRAG:
  42.     handle_drag(clamp(round(increment(previous_value(),newpos-ipos))));
  43.     return 1;
  44.   case FL_RELEASE:
  45.     handle_release();
  46.     return 1;
  47.   default:
  48.     return 0;
  49.   }
  50. }
  51.  
  52. void Fl_Roller::draw() {
  53.   if (damage()&FL_DAMAGE_ALL) draw_box();
  54.   int X = x()+Fl::box_dx(box());
  55.   int Y = y()+Fl::box_dy(box());
  56.   int W = w()-Fl::box_dw(box())-1;
  57.   int H = h()-Fl::box_dh(box())-1;
  58.   if (W<=0 || H <=0) return;
  59.   int offset = step() ? int(value()/step()) : 0;
  60.   const double ARC = 1.5; // 1/2 the number of radians visible
  61.   const double delta = .2; // radians per knurl
  62.   if (horizontal()) { // horizontal one
  63.     // draw shaded ends of wheel:
  64.     int h1 = W/4+1; // distance from end that shading starts
  65.     fl_color(color()); fl_rectf(X+h1,Y,W-2*h1,H);
  66.     for (int i=0; h1; i++) {
  67.       fl_color((Fl_Color)(FL_GRAY-i-1));
  68.       int h2 = FL_GRAY-i-1 > FL_DARK3 ? 2*h1/3+1 : 0;
  69.       fl_rectf(X+h2,Y,h1-h2,H);
  70.       fl_rectf(X+W-h1,Y,h1-h2,H);
  71.       h1 = h2;
  72.     }
  73.     if (active_r()) {
  74.       // draw ridges:
  75.       double junk;
  76.       for (double y = -ARC+modf(offset*sin(ARC)/(W/2)/delta,&junk)*delta;;
  77.        y += delta) {
  78.     int y1 = int((sin(y)/sin(ARC)+1)*W/2);
  79.     if (y1 <= 0) continue; else if (y1 >= W-1) break;
  80.     fl_color(FL_DARK3); fl_yxline(X+y1,Y+1,Y+H-1);
  81.     if (y < 0) y1--; else y1++;
  82.     fl_color(FL_LIGHT1);fl_yxline(X+y1,Y+1,Y+H-1);
  83.       }
  84.       // draw edges:
  85.       h1 = W/8+1; // distance from end the color inverts
  86.       fl_color(FL_DARK2);
  87.       fl_xyline(X+h1,Y+H-1,X+W-h1);
  88.       fl_color(FL_DARK3);
  89.       fl_yxline(X,Y+H,Y,X+h1);
  90.       fl_xyline(X+W-h1,Y,X+W);
  91.       fl_color(FL_LIGHT2);
  92.       fl_xyline(X+h1,Y-1,X+W-h1);
  93.       fl_yxline(X+W,Y,Y+H,X+W-h1);
  94.       fl_xyline(X+h1,Y+H,X);
  95.     }
  96.   } else { // vertical one
  97.     // draw shaded ends of wheel:
  98.     int h1 = H/4+1; // distance from end that shading starts
  99.     fl_color(color()); fl_rectf(X,Y+h1,W,H-2*h1);
  100.     for (int i=0; h1; i++) {
  101.       fl_color((Fl_Color)(FL_GRAY-i-1));
  102.       int h2 = FL_GRAY-i-1 > FL_DARK3 ? 2*h1/3+1 : 0;
  103.       fl_rectf(X,Y+h2,W,h1-h2);
  104.       fl_rectf(X,Y+H-h1,W,h1-h2);
  105.       h1 = h2;
  106.     }
  107.     if (active_r()) {
  108.       // draw ridges:
  109.       double junk;
  110.       for (double y = -ARC+modf(offset*sin(ARC)/(H/2)/delta,&junk)*delta;
  111.        ; y += delta) {
  112.     int y1 = int((sin(y)/sin(ARC)+1)*H/2);
  113.     if (y1 <= 0) continue; else if (y1 >= H-1) break;
  114.     fl_color(FL_DARK3); fl_xyline(X+1,Y+y1,X+W-1);
  115.     if (y < 0) y1--; else y1++;
  116.     fl_color(FL_LIGHT1);fl_xyline(X+1,Y+y1,X+W-1);
  117.       }
  118.       // draw edges:
  119.       h1 = H/8+1; // distance from end the color inverts
  120.       fl_color(FL_DARK2);
  121.       fl_yxline(X+W-1,Y+h1,Y+H-h1);
  122.       fl_color(FL_DARK3);
  123.       fl_xyline(X+W,Y,X,Y+h1);
  124.       fl_yxline(X,Y+H-h1,Y+H);
  125.       fl_color(FL_LIGHT2);
  126.       fl_yxline(X,Y+h1,Y+H-h1);
  127.       fl_xyline(X,Y+H,X+W,Y+H-h1);
  128.       fl_yxline(X+W,Y+h1,Y);
  129.     }
  130.   }
  131. }
  132.  
  133. Fl_Roller::Fl_Roller(int X,int Y,int W,int H,const char* L)
  134.   : Fl_Valuator(X,Y,W,H,L) {
  135.   box(FL_UP_FRAME);
  136.   step(1,1000);
  137. }
  138.  
  139. //
  140. // End of "$Id: Fl_Roller.cxx,v 1.6.2.1 1999/09/15 16:07:03 mike Exp $".
  141. //
  142.